package client

import (
	"context"
	"errors"
	"fmt"
	"gitee.com/enjoy_5/order-rpc/consts"
	"gitee.com/enjoy_5/order-rpc/proto/order"
	"gitee.com/xuxinzhi2206/corehub/nacos"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
	"log"
	"time"
)

func client(ctx context.Context, hand func(c order.OrderClient) error) error {
	service, err := nacos.FindService(consts.SERVICE_Name, "")
	if err != nil {
		panic(errors.New("服务获取错误"))
	}

	conn, err := grpc.NewClient(service, grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	fmt.Println(service)

	defer conn.Close()
	c := order.NewOrderClient(conn)

	ctx, cancel := context.WithTimeout(context.Background(), time.Second)
	defer cancel()

	return hand(c)
}

// 创建订单
func CreateOrder(ctx context.Context, in *order.CreateOrderRequest) (info *order.OrderInfo, err error) {
	err = client(ctx, func(c order.OrderClient) error {
		createOrder, err := c.CreateOrder(ctx, &order.CreateOrderRequest{In: in.GetIn()})
		if err != nil {
			return err
		}
		info = createOrder.GetInfo()
		return err
	})
	return
}

// 删除订单
func DeleteOrder(ctx context.Context, in *order.DeleteOrderRequest) error {
	return client(ctx, func(c order.OrderClient) error {
		_, err := c.DeleteOrder(ctx, &order.DeleteOrderRequest{
			UserId:  in.UserId,
			OrderId: in.OrderId,
		})
		if err != nil {
			return err
		}

		return err
	})
}

// 查询订单列表
func GetOrderList(ctx context.Context, in *order.OrderListRequest) (info []*order.OrderInfo, err error) {
	err = client(ctx, func(c order.OrderClient) error {
		list, err := c.OrderList(ctx, &order.OrderListRequest{
			UserId: in.UserId,
			Status: in.Status,
		})
		if err != nil {
			return err
		}
		info = list.GetInfo()
		return err
	})
	return
}

// 查询订单商品列表
func GetOrderGoodsList(ctx context.Context, in *order.OrderListInfoRequest) (info []*order.OrderGoodsInfo, err error) {
	err = client(ctx, func(c order.OrderClient) error {
		listInfo, err := c.OrderListInfo(ctx, &order.OrderListInfoRequest{OrderId: in.OrderId})
		if err != nil {
			return err
		}
		info = listInfo.GetInfo()
		return err
	})
	return
}

// 修改订单状态
func UpdateOrderStatus(ctx context.Context, in *order.OrderUpdateStatusRequest) error {
	return client(ctx, func(c order.OrderClient) error {
		_, err := c.OrderUpdateStatus(ctx, &order.OrderUpdateStatusRequest{
			OrderId: in.OrderId,
			Status:  in.Status,
		})
		if err != nil {
			return err
		}
		return err
	})
}

// 查询订单详情
func GetOrderInfo(ctx context.Context, in *order.GetOrderInfoRequest) (info *order.OrderInfo, err error) {
	err = client(ctx, func(c order.OrderClient) error {
		orderInfo, err := c.GetOrderInfo(ctx, &order.GetOrderInfoRequest{OrderId: in.OrderId})
		if err != nil {
			return err
		}
		info = orderInfo.GetInfo()
		return err
	})
	return
}

// 回滚创建订单
func RollbackOrderCreate(ctx context.Context, in *order.CreateOrderRequest) error {
	return client(ctx, func(c order.OrderClient) error {
		_, err := c.RollbackOrderCreate(ctx, &order.CreateOrderRequest{In: in.GetIn()})
		if err != nil {
			return err
		}
		return nil
	})
}
